const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            tabledata: [],
            Task_SerialNumber: "",
            // page: {
            //     PageNumber: 1, //页码
            //     RowAmount: 10,
            //     Condition: "",
            // },
            SendMessageCount: 0,
            Membertotal: 0
        };
    },
    mounted() {

        let ls_URL = window.location.href;
        this.Task_SerialNumber = getQueryObject(ls_URL, "nodeKey");

        this.GetTaskInfoAndSetPlanInfo();



    },
    methods: {


        GetTaskInfoAndSetPlanInfo() {


            $.ajax({

                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'patrol_task_Grid',
                    Condition: "Row_Status='0' and  Task_SerialNumber='" + this.Task_SerialNumber + "'",
                },
                success: (TaskInfo) => {
                    this.generateRange(TaskInfo);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",


            })


        },


        generateRange(TaskInfo) {

            const Task_SerialNumber = TaskInfo[0].Task_SerialNumber;
            const Task_Name = TaskInfo[0].Task_Name;
            let ObjTesk = { "Task_SerialNumber": Task_SerialNumber, "Task_Name": Task_Name };

            let _tabledata = [];
            let _this = this;

            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: "post",
                data: {
                    SQLSerialNumber: "SQL_Getgrid_information_patrol",
                    Condition: "",
                    ExtensionCondition: "",//window.btoa(unescape(encodeURIComponent(" and 1=1"))),
                },
                success: (Gridnfo) => {

                    for (let i = 0; i < Gridnfo.length; i++) {
                        let Objinfo = new Object;
                        Objinfo = { ...ObjTesk, ...Gridnfo[i] }
                        _tabledata.push(Objinfo);
                    }
                    _this.tabledata = _tabledata;
                    console.log(_this.tabledata);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            })

        },
        //提交表单
        async handleSubmit1() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                let ls_URL = window.location.href;
                const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

                let ls_MainTableName = lJSONObject_Data_Options.TableName;
                let ls_DataForm = lJSONObject_Data_Options.form;
                let ls_primary_key = lJSONObject_Data_Options.primary_key;
                //没有子表下面内容就会报错，try/catch抛出异常
                try {
                    //可能涉及多个子表
                    document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                        if (item.hasAttribute('data-options')) {
                            let lJSONObject_ChildData_Options = new Object();
                            lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                            let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                            let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
                            // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
                            if (this[ls_DataForm][ls_primary_key]) {
                                this[ls_ChildDataForm].forEach(item => {
                                    item[ls_primary_key] = this[ls_DataForm][ls_primary_key];
                                })
                            }
                            //建立子表结构体
                            let JSONArray_ChildRecord = new Array();
                            JSONArray_ChildRecord = this[ls_ChildDataForm];
                            if (!this[ls_DataForm].Children) {
                                this[ls_DataForm].Children = new Object();
                            }
                            this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
                        }
                    })
                } catch (error) {

                }
                //建立新增提交结构
                let lJSONObject_DataSet = new Object();
                if (action == 'Add') {
                    if (this.nodeKey) {
                        this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
                    }
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(this[ls_DataForm]);
                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
                    lJSONObject_DataSet[action] = lJSONObject_Table;
                } else if (action == 'Modify') {
                    const { add, modify } = this.deal(this[ls_DataForm]);
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(modify);

                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

                    lJSONObject_DataSet[action] = lJSONObject_Table;
                    lJSONObject_DataSet['Add'] = new Object();
                    if (add != null) {
                        let lJSONArray_Record_Add = new Array();
                        lJSONArray_Record_Add.push(add);

                        let lJSONObject_Table_Add = new Object();
                        lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

                        lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
                    }
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSaveByRequestBody",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        //给专职网格员发消息
                        this.SendMessageCount = 0;
                        //查询需要发送消息的网格员

                        $.ajax({

                            headers: requestHeaders(),
                            url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                            method: "post",
                            data: {
                                SQLSerialNumber: "SQL_patrol_taskPlan_Gridsendmessage", //sql名
                                Condition: "", //不必选的条件
                                ExtensionCondition: window.btoa(
                                    unescape(
                                        encodeURIComponent(
                                            " and patrol_taskPlan_Grid.Task_SerialNumber='" + this.Task_SerialNumber + "'"
                                        ))),
                            },
                            success: (data) => {
                                this.Membertotal = data.length;
                                this.SendMessageCount = 0;
                                if (this.Membertotal > 0) {
                                    data.map(item => {
                                        this.seedMessage(item);
                                    })

                                } else {
                                    // window.location.href = "./index_taskPlan.html?action=Query&nodeKey=" + this.Task_SerialNumber + "&team_SerialNumber=" + this.team_SerialNumber;
                                    window.location.href = './index.html';
                                }
                            },
                            complete: (XMLHttpRequest) => {
                                requestPretreatment(XMLHttpRequest);

                            },
                            dataType: "json",


                        })


                     
                    }



                    this.loading = false;
                })

            } else {
                this.$message({
                    message: '存在未填写的必填项！',
                    type: 'warning'
                });
            }
        },

          // 模板IDtP5n7jHkEiY-ay0kEBmn14wQNEnmNRUenefcMlKGE3s
        // 模板编号5771
        // 标题事项办理进度通知
        // 类目政务服务大厅
        // 操作人__****17 2025-09-19 添加
        // 详细内容申请人{{name1.DATA}}
        // 事项名称{{thing2.DATA}}
        // 当前进度{{thing3.DATA}}
        // 创建时间{{date7.DATA}}
        // 备注{{thing9.DATA}}
        // 场景说明事件类办理等
        seedMessage(itemtaskPlan_member) {
            //获取人员名称以及OPENID
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: "SQL_GetUserNameAndOpenID",
                    Condition: '',
                    ExtensionCondition: window.btoa(unescape(encodeURIComponent(" where personal_information.ID_Card_Number ='" + itemtaskPlan_member.ID_Card_Number + "' ")))
                },
                success: (res1) => {
                    if (res1.length > 0) {
                        const lopenid = res1[0].OpenID;
                        const lname = res1[0].Personal_Name;
                        const lServerdatetime = res1[0].Serverdatetime;
                        //调用消息发送接口
                        $.ajax({
                            headers: requestHeaders('JSON'),
                            url: `${mh}/requestForward/requestForwardForPost?RequestURL=${encodeURIComponent(`/wx/mp/sendMicroProgramTemplateMessage`)}`,
                            method: "POST",
                            data: JSON.stringify({
                                openId: lopenid,
                                templateId: "tP5n7jHkEiY-ay0kEBmn14wQNEnmNRUenefcMlKGE3s",
                                content: {
                                    name1: { value: "网格巡查系统" },
                                    thing2: { value: "网格号：" + itemtaskPlan_member.Grid_SerialNumber },
                                    thing3: { value: '任务下发，待完成' },
                                    date7: { value: lServerdatetime },
                                    thing9: { value: "任务号：" + this.Task_SerialNumber },
                                }
                            }),
                            success: (res) => {

                            },
                            complete: (XMLHttpRequest) => {
                                requestPretreatment(XMLHttpRequest)
                                this.SendMessageCount++;
                                if (this.SendMessageCount == this.Membertotal) {
                                       window.location.href = './index.html';
                                }
                            },

                        });
                    } else {
                        this.SendMessageCount++;
                        if (this.SendMessageCount == this.Membertotal) {
                            window.location.href = './index.html';
                        }
                    }
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });



        },

        // handleSizeChange(val) {
        //     this.page.RowAmount = val;
        //     this.page.PageNumber = 1; // 重置为第一页
        // },
        // handleCurrentChange(val) {
        //     this.page.PageNumber = val;
        // },

    },

};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-select', ylSelect);
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
